package com.lsa.helpers;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import junit.framework.TestCase;

public class UtilsTest extends TestCase
{
	public void testSimultaneousMinMax() {
		{
			Pair<Integer, Integer> minMax = Utils.minMax(Arrays.asList(5, 3, 6,7,1));
			assertEquals(""+minMax, minMax, Pair.create(1, 7));
		}
		
		{
			Pair<Integer, Integer> minMax = Utils.minMax(Arrays.asList(5, 3, 6,7));
			assertEquals(""+minMax, minMax, Pair.create(3, 7));
		}
		
		{
			Pair<Integer, Integer> minMax = Utils.minMax(Arrays.asList(5, 5, 5,5));
			assertEquals(""+minMax, minMax, Pair.create(5, 5));
		}
		
		{
			Pair<Integer, Integer> minMax = Utils.minMax(Arrays.asList(5, 5, 5));
			assertEquals(""+minMax, minMax, Pair.create(5, 5));
		}
		
		{
			Pair<Integer, Integer> minMax = Utils.minMax(Arrays.asList(5, 5));
			assertEquals(""+minMax, minMax, Pair.create(5, 5));
		}
		
		{
			Pair<Integer, Integer> minMax = Utils.minMax(Arrays.asList(5));
			assertEquals(""+minMax, minMax, Pair.create(5, 5));
		}
		
		{
			List<Integer> l = Arrays.asList(2,6,13,25,17,18,25,20,20,20);
			Collections.shuffle(l);
			Pair<Integer, Integer> minMax = Utils.minMax(l);
			assertEquals(""+minMax, minMax, Pair.create(2, 25));
		}
		
		{
			List<Integer> l = Utils.randomList(100, 1, 1000);
			Pair<Integer, Integer> minMax = Utils.minMax(l);
			assertEquals(""+minMax + " " + l, minMax, Pair.create(Collections.min(l), Collections.max(l)));
		}
	}
	
	public void testMovePart()
	{
		int[] array = new int[]{8, 9, 1, 12, 1, 2, 5, 8, 9, 4};
		array = Utils.moveSlice(array, 1, 1);
		int[] testArray = Utils.createIntArray1D("8 9 9 1 12 1 2 5 8 9");
		assertEquals(true, Utils.compareArrays(array, testArray));
		
		array = new int[]{8, 9, 1, 12, 1, 2, 5, 8, 9, 4};
		array = Utils.moveSlice(array, 1, 4);
		testArray = Utils.createIntArray1D("8 9 1 12 1 9 1 12 1 2");
		assertEquals(true, Utils.compareArrays(array, testArray));
		
		array = new int[]{8, 9, 1, 12, 1, 2, 5, 8, 9, 4};
		array = Utils.moveSlice(array, 0, 1);
		testArray = Utils.createIntArray1D("8 8 9 1 12 1 2 5 8 9");
		assertEquals(true, Utils.compareArrays(array, testArray));
	}
	
	public void testMoveElement()
	{
		int[] array = Utils.createIntArray1D("8 9 1 7 1 2 5 8 9 4");
		array = Utils.moveElement(array, 3, 1);
		int[] testArray = Utils.createIntArray1D("8 7 9 1 1 2 5 8 9 4");
		assertEquals(true, Utils.compareArrays(array, testArray));
		
		array = Utils.createIntArray1D("8 9 1 7 1 2 5 8 9 4");
		array = Utils.moveElement(array, 1, 7);
		testArray = Utils.createIntArray1D("8 1 7 1 2 5 8 9 9 4");
		assertEquals(true, Utils.compareArrays(array, testArray));
		
		array = Utils.createIntArray1D("8 9 1 7 1 2 5 8 9 4");
		array = Utils.moveElement(array, 3, 4);
		testArray = Utils.createIntArray1D("8 9 1 1 7 2 5 8 9 4");
		assertEquals(true, Utils.compareArrays(array, testArray));
		
		array = Utils.createIntArray1D("8 9 1 7 1 2 5 8 9 4");
		array = Utils.moveElement(array, 1, 3);
		testArray = Utils.createIntArray1D("8 1 7 9 1 2 5 8 9 4");
		assertEquals(true, Utils.compareArrays(array, testArray));
	}
	
	public static void testRandom()
    {
        Map<Integer,Integer> stat = new TreeMap<Integer, Integer>();
        
        int count = 1000000;
        int lower = 1;
        int upper = 5;
        
        for( int i = lower; i <= upper; i++ )
        {
            stat.put(i, 0);
        }
        
        for (int i = 0; i <= count; i++)
        {
            int r = Utils.random(lower, upper);
            Integer cnt = stat.get(r);
            cnt++;
            stat.put(r, cnt);
        }
        
        printStat(stat, count);
    }
    
    public static void printStat(Map<Integer, Integer> stat, int count)
    {
        for( Iterator iterator = stat.entrySet().iterator(); iterator.hasNext(); )
        {
            Map.Entry<Integer, Integer> pair= (Map.Entry<Integer, Integer>)iterator.next();
            float percent = (((float)pair.getValue()) / (float)count) * 100.0f; 
            System.out.println(""+pair.getKey()+":"+pair.getValue() + ":" + percent + "%");
        }
    }
}
